const std = @import("std");
const httpz = @import("httpz");
const cate_server = @import("../server/cate_server.zig");
const myjwt = @import("../utils/jwt.zig");
const const_val = @import("../utils/const_value.zig");
const cate_model = @import("../model/cate.zig");

/// 获取首页分类列表
pub fn getHomeCategories(req: *httpz.Request, res: *httpz.Response) !void {
    _ = req;
    const cates = try cate_server.getHomeCategories();
    res.status = 200;
    try res.json(.{ .code = 200, .msg = "ok", .data = cates }, .{});
}

/// 获取分类列表
pub fn getCategoryList(req: *httpz.Request, res: *httpz.Response) !void {
    const token = req.header("authorization") orelse "";
    if (token.len == 0) {
        res.status = 401;
        return try res.json(.{ .code = 401, .msg = "未登录" }, .{});
    }

    const exp = myjwt.ExpectedPayload{ .iss = const_val.JWT_ISS, .sub = const_val.JWT_SUB };
    const is_ok = myjwt.verifyToken(exp, token) catch false;
    if (!is_ok) {
        res.status = 401;
        return try res.json(.{ .code = 401, .msg = "token验证失败" }, .{});
    }

    const cates = try cate_server.getCategories();
    res.status = 200;
    try res.json(.{ .code = 200, .msg = "ok", .data = cates }, .{});
}

/// 添加分类
pub fn addCategory(req: *httpz.Request, res: *httpz.Response) !void {
    const token = req.header("authorization") orelse "";
    if (token.len == 0) {
        res.status = 401;
        return try res.json(.{ .code = 401, .msg = "未登录" }, .{});
    }

    const exp = myjwt.ExpectedPayload{ .iss = const_val.JWT_ISS, .sub = const_val.JWT_SUB };
    const is_ok = myjwt.verifyToken(exp, token) catch false;
    if (!is_ok) {
        res.status = 401;
        return try res.json(.{ .code = 401, .msg = "token验证失败" }, .{});
    }

    const post_data = try req.json(cate_model.CategoryDtoForCreate);
    if (post_data) |cate| {
        const is_add = try cate_server.addCategory(cate);
        if (!is_add) {
            res.status = 500;
            return try res.json(.{ .code = 500, .msg = "添加分类失败" }, .{});
        }

        res.status = 201;
        try res.json(.{ .code = 200, .msg = "ok", .data = null }, .{});
    } else {
        res.status = 400;
        return try res.json(.{ .code = 400, .msg = "错误的请求" }, .{});
    }
}

/// 编辑分类
pub fn editCategory(req: *httpz.Request, res: *httpz.Response) !void {
    const token = req.header("authorization") orelse "";
    if (token.len == 0) {
        res.status = 401;
        return try res.json(.{ .code = 401, .msg = "未登录" }, .{});
    }

    const exp = myjwt.ExpectedPayload{ .iss = const_val.JWT_ISS, .sub = const_val.JWT_SUB };
    const is_ok = myjwt.verifyToken(exp, token) catch false;
    if (!is_ok) {
        res.status = 401;
        return try res.json(.{ .code = 401, .msg = "token验证失败" }, .{});
    }

    const post_data = try req.json(cate_model.CategoryDtoForEdit);
    if (post_data) |cate| {
        const is_edit = try cate_server.editCategory(cate);
        if (!is_edit) {
            res.status = 500;
            return try res.json(.{ .code = 500, .msg = "编辑分类失败" }, .{});
        }

        res.status = 200;
        try res.json(.{ .code = 200, .msg = "ok", .data = null }, .{});
    } else {
        res.status = 400;
        return try res.json(.{ .code = 400, .msg = "错误的请求" }, .{});
    }
}

/// 删除分类
pub fn deleteCategoryById(req: *httpz.Request, res: *httpz.Response) !void {
    const id_str = req.param("id") orelse "";
    if (id_str.len == 0) {
        res.status = 400;
        try res.json(.{ .code = 400, .msg = "错误的请求" }, .{});
        return;
    }

    const token = req.header("authorization") orelse "";
    if (token.len == 0) {
        res.status = 401;
        return try res.json(.{ .code = 401, .msg = "未登录" }, .{});
    }

    const exp = myjwt.ExpectedPayload{ .iss = const_val.JWT_ISS, .sub = const_val.JWT_SUB };
    const is_ok = myjwt.verifyToken(exp, token) catch false;
    if (!is_ok) {
        res.status = 401;
        return try res.json(.{ .code = 401, .msg = "token验证失败" }, .{});
    }

    const id = std.fmt.parseInt(u32, id_str, 10) catch 0;
    const is_del = try cate_server.deleteCategoryById(id);
    if (!is_del) {
        res.status = 500;
        return try res.json(.{ .code = 500, .msg = "删除分类失败" }, .{});
    }

    res.status = 200;
    try res.json(.{ .code = 200, .msg = "ok", .data = null }, .{});
}

/// 批量删除分类
pub fn deleteCategoryBatch(req: *httpz.Request, res: *httpz.Response) !void {
    const token = req.header("authorization") orelse "";
    if (token.len == 0) {
        res.status = 401;
        return try res.json(.{ .code = 401, .msg = "未登录" }, .{});
    }

    const exp = myjwt.ExpectedPayload{ .iss = const_val.JWT_ISS, .sub = const_val.JWT_SUB };
    const is_ok = myjwt.verifyToken(exp, token) catch false;
    if (!is_ok) {
        res.status = 401;
        return try res.json(.{ .code = 401, .msg = "token验证失败" }, .{});
    }

    const PostData = struct {
        ids: []u32,
    };

    const post_data = req.json(PostData) catch {
        res.status = 400;
        return try res.json(.{ .code = 400, .msg = "错误的请求" }, .{});
    };

    if (post_data) |post_ids| {
        const is_del = try cate_server.deleteCategoryBatch(post_ids.ids);
        if (!is_del) {
            res.status = 500;
            return try res.json(.{ .code = 500, .msg = "批量删除分类失败" }, .{});
        }

        res.status = 200;
        try res.json(.{ .code = 200, .msg = "ok", .data = null }, .{});
    } else {
        res.status = 400;
        return try res.json(.{ .code = 400, .msg = "错误的请求" }, .{});
    }
}
